package com.jinhua.admin.controller;

import com.jinhua.admin.service.OrderService;
import com.jinhua.admin.vo.OrderVo;
import com.jinhua.config.IdUtil;
import com.jinhua.config.PaginationRequest;
import com.jinhua.config.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
* 订单表的操作接口
*/
@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
    * 添加或者修改订单表
    *
    * @return
    */
    @PostMapping("save")
    public Response saveOrder(@RequestBody OrderVo orderVo) {
        if (orderVo == null) {
            return Response.error();
        }
        return Response.success(orderService.saveOrder(orderVo));
    }

    /**
    * 删除订单表
    *
    * @param ids 订单表的Id的集合,例如1,2,3多个用英文,隔开

    * @return
    */
    @RequestMapping("del")
    public Response delOrder(Long[] ids) {
        if (ids == null || ids.length == 0) {
            return Response.error();
        }
        return Response.success(orderService.delOrderByIds(ids));
    }

    /**
    * 查找订单表
    *
    * @return
    */
    @RequestMapping("find")
    public Response findOrder(@RequestBody @Valid PaginationRequest<OrderVo> request ) {
        if (request == null) {
            return Response.error();
        }
        OrderVo  orderVo = request.getQueryCriteria();
        if(orderVo.getId()!=null){
            return Response.success(orderService.findOrderById(orderVo.getId()));
        }
        return Response.success(orderService.findOrderByAll(request), IdUtil.getTotal());
    }
}
